﻿Imports MySql.Data.MySqlClient
Public Class Form4
    Dim cmd As New MySqlCommand
    Dim da As New MySqlDataAdapter
    Dim con As MySqlConnection = myconn()
    Public Function myconn() As MySqlConnection
        Return (New MySqlConnection("server=localhost;user id=root;password=;database=vb-librarysys"))
    End Function
    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GroupBox1.Enabled = True
    End Sub

    Private Sub lbllogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbllogin.Click
        If lbllogin.Text = "logout" Then
            lbllogin.Text = "Login"
            lblname.Text = "Hi, Guest!"
        ElseIf lbllogin.Text = "Login" Then
            GroupBox1.Enabled = True

        End If

    End Sub

    Private Sub btnok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click
        Dim sql As String
        Dim publictable As New DataTable
        Try

            If txtUname.Text = "" And txtPass.Text = "" Then
                MsgBox("Password or Username Incorrect!")

            Else
                sql = "select * from user where username ='" & txtUname.Text & "' and userpassword = '" & txtPass.Text & "'"

                With (cmd)
                    .Connection = con
                    .CommandText = sql
                End With
                da.SelectCommand = cmd
                da.Fill(publictable)

                If publictable.Rows.Count > 0 Then
                    Dim user_type, name As String
                    user_type = publictable.Rows(0).Item(4)
                    name = publictable.Rows(0).Item(2)
                    If user_type = "Admin" Then
                        MsgBox("Welcome " & name & " you login as Administrator ")
                        lbllogin.Text = "Logout"
                        GroupBox1.Enabled = False
                        txtUname.Text = ""
                        txtPass.Text = ""
                        lblname.Text = "Welcome, " & name
                        form1.Show()

                    ElseIf user_type = "Encoder" Then
                        MsgBox("Welcome " & name & " you login as Encoder ")
                        lbllogin.Text = "Logout"
                        GroupBox1.Enabled = False
                        txtUname.Text = ""
                        txtPass.Text = ""
                        lblname.Text = "Hi, " & name
                        form1.Show()
                    Else
                        MsgBox("You login as Guest!")
                        lbllogin.Text = "Logout"
                        GroupBox1.Enabled = False
                        txtUname.Text = ""
                        txtPass.Text = ""
                        lblname.Text = "Hi, " & name
                        form1.Show()
                    End If
                Else
                    MsgBox("Contact administrator to registered!")
                    txtUname.Text = ""
                    txtPass.Text = ""
            End If

            da.Dispose()
            End If

    Catch ex As Exception
            MsgBox(ex.Message)

        End Try
        con.Clone()

    End Sub
  
    Private Sub btncancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancel.Click
        Me.Close()
    End Sub
End Class